package com.coolH.controller;

import com.coolH.entity.Student;
import com.coolH.service.StudentService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by hbl on 2017/6/2.
 */
@Controller
@RequestMapping("/Student")
public class StudentController {
    private static final Logger LOGGER = Logger.getLogger(StudentController.class);
    @Autowired
    StudentService studentService;

    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public String register(String userName, String userNickname,String userNumber,String userPassword){
        LOGGER.info(userName+">>>>>>>>>>"+userNickname+">>>>>>>>>>>"+userNumber+">>>>>>>>"+userPassword);
        Student student = new Student();
        student.setUsername(userName);
        student.setUsernickname(userNickname);
        student.setUsernumber(userNumber);
        student.setUserpassword(userPassword);
        LOGGER.info(student.getUsername() + "<<<<<<<<<<<<<<<<<");
        studentService.Add(student);
        return "Login";
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(String userNumber,String password,HttpSession session,Model model){

        Student student = studentService.login(userNumber, password);
        session.setAttribute("student", student);
        return "redirect:/Student/list";

    }
    @RequestMapping(value = "/getOwn",method = RequestMethod.GET)
    public String getOwn(HttpSession session,Model model){
        if (!((session.getAttribute("student")) ==null)){
            return "redirect:/Student/list";
        }else {
            return "error";
        }

    }
    @ResponseBody
    @RequestMapping(value = "/modify",method = RequestMethod.GET)
    public void modify(String usernickname,String sign,HttpSession session){
        Student student = (Student) session.getAttribute("student");
        int id = student.getId();
        Student student1 = new Student();
        student1.setId(id);
        student1.setUsernickname(usernickname);
        student1.setSign(sign);
        LOGGER.info("id>>"+student1.getId()+"<<<<<<<<<<<<<<<<<<"+"name>>>"+student1.getUsernickname()+">>>>>>>>>>>>>"+sign);
        studentService.modifInfo(student1);
    }
    @RequestMapping(value = "/list")
    public  String Student(HttpSession session,Model model){
        model.addAttribute("student", session.getAttribute("student"));
        return "student";
    }
    @RequestMapping(value = "/setting")
    public  String setting(HttpSession session,Model model){
        model.addAttribute("student", session.getAttribute("student"));
        return "setting";
    }
    @RequestMapping(value = "/files")
    public  String files(HttpSession session,Model model){
        model.addAttribute("student", session.getAttribute("student"));
        return "Files";
    }
    @RequestMapping(value = "/logout")
    public  String logout(HttpSession session){
        session.removeAttribute("student");
        return "logout";
    }
}
